package net.tngou.crawler.drug.prescript;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.pojo.Prescript;
import net.tngou.crawler.util.StringUtil;

public class BaseCrawler {

	private static Long getId(String engines)
	{
		CacheEngine engine = new TgCacheEngine();//创建缓存    
		 Long id=(Long) engine.get(engines);
		 id=id==null?1:id+1;
		 engine.add(engines,  id);//存在  默认的储存库为 default
		 return id;	 
	}
	public static void jianke(Long id) {
	
		String url="https://www.jianke.com/wcgi/promos/promosCombination";
		Map<String, String> data = new HashMap<>();
		data.put("mainSkuCode", id+"");
		 String doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))doc = HttpUtil.getString(url, data );
		 if(StringUtil.isEmpty(doc))return;
		JSONArray jsons = JSON.parseArray(doc);
		for (int i = 0; i < jsons.size(); i++) {
			Prescript prescript = new Prescript();
			JSONObject json = jsons.getJSONObject(i);
			String name=json.getString("combination_name");
			String detail="药方："+json.getString("combination_detail")+"。 ";
	
			JSONArray ls = json.getJSONArray("promoCombinationRelations");
			for (int j = 0; j < ls.size(); j++) {
				JSONObject l = ls.getJSONObject(j);
				detail=detail+l.getString("sub_sku_name")+"—"+l.getString("introduction");
			}
			String introduction=json.getString("introduction");
			introduction="说明："+Jsoup.parse(introduction).text();
			prescript.setName(name);
			prescript.setDescription(detail+" "+introduction);
			prescript.save();
		}
	
	}
	
	public static void _360kad(Long id) {
		String url="http://www.360kad.com/product/KitList";
		Map<String, String> data = new HashMap<>();
		data.put("id", ""+id);
		 String doc = HttpUtil.getString(url, data );
		 JSONObject rjson=JSON.parseObject(doc);
		 JSONArray jsons = rjson.getJSONArray("Result");
		 for (int i = 0; i < jsons.size(); i++) {
			 Prescript prescript = new Prescript();
			 JSONObject json = jsons.getJSONObject(i);
			 String name=json.getString("PrmSubtitle");
			 String detail="";
			 if(StringUtil.isNotEmpty(json.getString("PrmDesc")))
				 detail=json.getString("PrmDesc").replace(name, "药方")+"。 ";
			 JSONArray ls = json.getJSONArray("kitSubList");
				for (int j = 0; j < ls.size(); j++) {
					JSONObject l = ls.getJSONObject(j);
					detail=detail+l.getString("WareName")+"。";
				}
			 String introduction = "说明："+json.getString("Remark");
			 prescript.setName(name);
			 prescript.setDescription(detail+" "+introduction);
			 prescript.save();
		 }
	}
	
	
	
	public static void main(String[] args) {
		
		while (true) {
			jianke(getId("jianke"));
			_360kad(getId("_360kad"));
			
		}	
			 
	}
}
